package com.org.gsc.shuaxindemo;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private XRecyclerView mXRecyclerView;
    private List<String> mttiles=new ArrayList<>();
    private MyAdapter mMyAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mXRecyclerView= (XRecyclerView) findViewById(R.id.xrv);
        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        for(int i=0;i<100;i++){
            mttiles.add(" 第"+(i+1)+"个数据");

        }
        mMyAdapter = new MyAdapter(mttiles, this);
        mXRecyclerView.setAdapter(mMyAdapter);
        mXRecyclerView.setRefreshProgressStyle(ProgressStyle.BallRotate);
        mXRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
      // View view=View.inflate(this,R.layout.head
      // ,null);
        View view=LayoutInflater.from(this).inflate(R.layout.head,mXRecyclerView,false);
       // View view= LayoutInflater.from(this).inflate(R.layout.head,(ViewGroup) findViewById(android.R.id.content),false);

         mXRecyclerView.addHeaderView(view);
        mMyAdapter.setIListeners(new IListeners() {
            @Override
            public void onItemclick(View view, int pos) {
                Toast.makeText(getApplicationContext(),"onItemclick"+pos,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void OnLongItemClick(View view, int pos) {
                Toast.makeText(getApplicationContext(),"OnLongItemClick"+pos,Toast.LENGTH_SHORT).show();
            }
        });
        mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
         @Override
         public void onRefresh() {
             System.out.println("--onRefresh()--");
             new Handler().postDelayed(new Runnable() {
                 @Override
                 public void run() {
                     //mAdapter.notifyDataSetChanged();
                     for(int i=0;i<20;i++){
                         mttiles.add("onRefresh()"+i);

                     }
                     mMyAdapter.notifyDataSetChanged();
                     mXRecyclerView.refreshComplete();
                 }
             },2000);

         }
         @Override
         public void onLoadMore() {
             System.out.println("--onLoadMore()--");
             new Handler().postDelayed(new Runnable() {
                 @Override
                 public void run() {
                     for(int i=0;i<30;i++){
                         mttiles.add(" onLoadMore()"+i);

                     }
                     mMyAdapter.notifyDataSetChanged();
                     //mAdapter.notifyDataSetChanged();
                     mXRecyclerView.loadMoreComplete();
                 }
             },2000);

         }
     });
    }
}
